import socket
import argparse
import sys

def client_program(message):
    # 创建socket对象
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 连接到服务端
    server_ip = '192.144.164.226'
    server_port = 50244
    try:
        client_socket.connect((server_ip, server_port))
        print(f"已链接至 {server_ip}:{server_port}")
        
        # 发送消息
        client_socket.sendall(message.encode())
        print(f"发送了消息: {message}")
        
        # 可选：接收响应
        # response = client_socket.recv(1024)
        # print(f"Received response: {response.decode()}")
    except Exception as e:
        print(f"Error: {e}")
    finally:
        # 关闭连接
        client_socket.close()
        print("Connection closed")

if __name__ == '__main__':
    # 解析命令行参数
    parser = argparse.ArgumentParser(description="Send a message to the server.")
    parser.add_argument('-txt', type=str, help='The message to send to the server', required=True)
    args = parser.parse_args()

    # 获取要发送的消息
    message = args.txt

    # 调用客户端程序
    client_program(message)
